import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_mvp/l10n/app_localizations.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';
import 'LocaleProvider.dart';
import 'main.dart';
/**
 * 登录界面
 */
class Login extends StatefulWidget {
  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
  final _formKey = GlobalKey<FormState>();
  bool _isPasswordVisible = false;
  late String _username;
  late String _password;

  void _login() {
    HttpOverrides.global = MyHttpOverrides();
    setState(() {
      _formKey.currentState!.save();
    });
    if (_formKey.currentState!.validate()) {
      // 触发登录逻辑
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(SnackBar(content: Text('登录中...用户名：$_username'),
          duration: Duration(seconds: 1)));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        actions: [
          PopupMenuButton<Locale>(
            position: PopupMenuPosition.over,
            offset: const Offset(0, 20),
            itemBuilder: (context) =>
            [
              PopupMenuItem(
                value: const Locale('en'),
                child: Text("English"),
              ),
              PopupMenuItem(
                value: const Locale('zh'),
                child: Text("中文"),
              ),
            ],
            onSelected: (Locale locale) {//设置语言选择
              final localeProvider = Provider.of<LocaleProvider>(context, listen: false);
              localeProvider.setLocale(locale);
            },
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 8.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(Icons.language, size: 20, color: Colors.black),
                  SizedBox(width: 3),
                  Text(
                    "语言切换",
                    style: TextStyle(fontSize: 16, color: Colors.black),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: Column(
            children: [
              SizedBox(height: 75),
              Column(
                children: [
                  SvgPicture.asset(
                    "assets/images/ad.svg",
                    width: 76,
                    height: 76,
                  ),
                  Text(
                    "HaiPower",
                    style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
                  ),
                ],
              ),
              TextFormField(
                decoration: InputDecoration(labelText: AppLocalizations.of(context)!.username),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return AppLocalizations.of(context)!.peleseUsername;
                  }
                  return null;
                },
                onSaved: (value) {
                  _username = value!;
                },
              ),
              SizedBox(height: 16),
              TextFormField(
                decoration: InputDecoration(labelText: AppLocalizations.of(context)!.password,
                  suffixIcon: IconButton(
                    icon: Icon(
                      _isPasswordVisible ? Icons.visibility : Icons
                          .visibility_off,
                      color: Colors.grey,
                    ),
                    onPressed: () {
                      setState(() {
                        _isPasswordVisible = !_isPasswordVisible;
                      });
                    },
                  ),),
                obscureText: !_isPasswordVisible,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return AppLocalizations.of(context)!.pelesePassword;
                  }
                  return null;
                },
                onSaved: (value) {
                  _password = value!;
                },
              ),
              SizedBox(height: 24),
              ElevatedButton(
                onPressed: _login,
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.blue,
                  minimumSize: Size(double.infinity, 50),
                ),
                child: Text(AppLocalizations.of(context)!.loginButton,
                  style: TextStyle(color: Colors.white, fontSize: 18),),
              ),
              SizedBox(height: 16),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  TextButton(
                    onPressed: () {
                      // 跳转到注册页
                    },
                    child: Text(AppLocalizations.of(context)!.notHaveAccount,
                        style: TextStyle(color: Colors.blue, fontSize: 16)),
                  ),
                  TextButton(
                    onPressed: () {
                      // 忘记密码
                    },
                    child: Text(AppLocalizations.of(context)!.forgetPassword,
                        style: TextStyle(color: Colors.blue, fontSize: 16)),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
      bottomNavigationBar: Container(
        padding: const EdgeInsets.all(16.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            // 左侧：图标 + 文字
            _buildLabelWithBorder(Icons.language, AppLocalizations.of(context)!.locationTool),
            _buildLabelWithBorder(Icons.help_outline, AppLocalizations.of(context)!.demoStation),
          ],
        ),
      ),
    );
  }

  Widget _buildLabelWithBorder(IconData icon, String text) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey, width: 0.8),
        borderRadius: BorderRadius.circular(20), // 圆角矩形边框
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(icon, size: 20, color: Colors.black),
          SizedBox(width: 8),
          Text(text, style: TextStyle(fontSize: 16, color: Colors.blue)),
        ],
      ),
    );
  }
}
